package com.ruoyi.supplier.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.supplier.domain.bo.UndoLogBo;
import com.ruoyi.supplier.domain.vo.UndoLogVo;
import com.ruoyi.supplier.domain.UndoLog;
import com.ruoyi.supplier.mapper.UndoLogMapper;
import com.ruoyi.supplier.service.IUndoLogService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * AT transaction mode undo tableService业务层处理
 *
 * @author ruoyi
 * @date 2023-09-18
 */
@RequiredArgsConstructor
@Service
@DS("slave")
public class UndoLogServiceImpl implements IUndoLogService {

    private final UndoLogMapper baseMapper;

    /**
     * 查询AT transaction mode undo table
     */
    @Override
    public UndoLogVo queryById(Long branchId){
        return baseMapper.selectVoById(branchId);
    }

    /**
     * 查询AT transaction mode undo table列表
     */
    @Override
    public TableDataInfo<UndoLogVo> queryPageList(UndoLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<UndoLog> lqw = buildQueryWrapper(bo);
        Page<UndoLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询AT transaction mode undo table列表
     */
    @Override
    public List<UndoLogVo> queryList(UndoLogBo bo) {
        LambdaQueryWrapper<UndoLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<UndoLog> buildQueryWrapper(UndoLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<UndoLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getContext()), UndoLog::getContext, bo.getContext());
        lqw.eq(StringUtils.isNotBlank(bo.getRollbackInfo()), UndoLog::getRollbackInfo, bo.getRollbackInfo());
        lqw.eq(bo.getLogStatus() != null, UndoLog::getLogStatus, bo.getLogStatus());
        lqw.eq(bo.getLogCreated() != null, UndoLog::getLogCreated, bo.getLogCreated());
        lqw.eq(bo.getLogModified() != null, UndoLog::getLogModified, bo.getLogModified());
        return lqw;
    }

    /**
     * 新增AT transaction mode undo table
     */
    @Override
    public Boolean insertByBo(UndoLogBo bo) {
        UndoLog add = BeanUtil.toBean(bo, UndoLog.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setBranchId(add.getBranchId());
        }
        return flag;
    }

    /**
     * 修改AT transaction mode undo table
     */
    @Override
    public Boolean updateByBo(UndoLogBo bo) {
        UndoLog update = BeanUtil.toBean(bo, UndoLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(UndoLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除AT transaction mode undo table
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
